<?php
/**
 * Created by PhpStorm.
 * User: mikol
 * Date: 2018/8/2 0002
 * Time: 16:02
 */

namespace app\api\controller;

use think\cache\driver\Redis;

class Base
{
    protected $appId = 'wx33ec2270b1eab69c';
    protected $secret = '891070201dc0579797a3fe83edf4f303';
    public $redis;

    public function __construct()
    {
        $this->redis = new Redis();
        $this->redis = $this->redis->handler();
        if ($this->redis->EXISTS('access_token')){
            return $this->redis->get('access_token');
        }else{
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->secret;
//        echo $url;die;
            $data = getUrl($url);
            $this->redis->setex('access_token',7200,$data->access_token);
            return $data->access_token;
        }
    }

    /**
     * 获取access_token
     * @return mixed
     */
//    public function getAccessToken()
//    {
//        if ($this->redis->EXISTS('access_token')){
//            return $this->redis->get('access_token');
//        }else{
//            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->secret;
////        echo $url;die;
//            $data = getUrl($url);
//            $this->redis->setex('access_token',7200,$data->access_token);
//            return $data->access_token;
//        }
//
//    }

}